angular.module('app')
    .component('audioShow', {
        templateUrl: './audio-show.component.html',
        bindings: {
            value: '<?'
        },
        controller: function ($scope, $element, commonUtil,$timeout) {
            var $ctrl = this;
            var customTimeout,timeCatch=0;
            $ctrl.$onInit = function () {
                if(!RongIMLib){
                    var script = document.createElement("script");
                    script.src = '../resources/js/audioInit.js';
                    document.head.appendChild(script);
                }
            };

            $scope.$watch('$ctrl.value',function(){
                if($ctrl.value){
                    var fr = new FileReader();
                    fr.onloadend = function (e) {
                        $ctrl.base64Str = e.target.result;
                        $ctrl.audioLength =Math.round($ctrl.base64Str.length/2000);
                        $ctrl.allTime = returnCountDown($ctrl.audioLength)
                    };
                    var request = new XMLHttpRequest();
                    request.responseType = "blob";
                    request.open("get", $ctrl.value);
                    request.send(null);
                    request.onload = function () {
                        if (request.status == 200) {
                            fr.readAsDataURL(this.response);
                        }
                    }
                }
            })
            function returnCountDown(s){
                var t,d,m,k,l;
                d = Math.floor(s/3600/24);
                m = Math.floor(s%(24*3600)/3600);
                if(m<10){
                    m = '0'+m;
                }
                k = Math.floor(s%3600/60);
                if(k<10){
                    k = '0'+k;
                }
                l = Math.round(s%60);
                if(l<10){
                    l = '0'+l;
                }
                if(m != '00'){
                    return d?(d+'天 '+m+':'+k+':'+l):(m+':'+k+':'+l)
                }else{
                    return k+':'+l;
                }
            }
            function countDown(time){
                $ctrl.nowTime = returnCountDown(time);
                $ctrl.now = (time/$ctrl.audioLength)*100+'%'
                commonUtil.safeApply();
                customTimeout = $timeout(function(){
                    time = time+0.1;
                    timeCatch = time;
                    if(time <=$ctrl.audioLength){
                        countDown(time);
                    }else{
                        $timeout.cancel(customTimeout);
                        $ctrl.begin = false;
                        timeCatch = 0;
                    }
                },100)
            }
            $ctrl.play = function(){
                $ctrl.begin = true;
                RongIMLib.RongIMVoice.play($ctrl.base64Str);
                if(timeCatch >= $ctrl.audioLength){
                    timeCatch = 0;
                }
                customTimeout = countDown(timeCatch);
            }

            $ctrl.stop = function(){
                $timeout.cancel(customTimeout);
                $ctrl.begin = false;
                RongIMLib.RongIMVoice.stop();
            }
        }
    });